//
//  ViewController.m
//  MusicDemo
//
//  Created by zhiyou on 16-5-26.
//  Copyright (c) 2016年 韩少波. All rights reserved.
//

#import "ViewController.h"
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>

@interface ViewController (){
    NSTimer *_timer;
}
//音乐播放器对象
@property (nonatomic, strong) AVAudioPlayer *play;
@property (weak, nonatomic) IBOutlet UISlider *slider;
@end

@implementation ViewController

- (void)createPlay:(NSString *)url {
    NSURL *fielUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:url ofType:nil] ] ;
    self.play = [[AVAudioPlayer alloc]initWithContentsOfURL:fielUrl error:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self createPlay:@"雨一直下.mp3"];
    
    
}

//音效
/**
 *  使用System Sound Service 播放音效的步骤如下：
 
    调用AudioServicesCreateSystemSoundID(   CFURLRef  inFileURL, SystemSoundID*   outSystemSoundID)函数获得系统声音ID。
    如果需要监听播放完成操作，则使用AudioServicesAddSystemSoundCompletion(  SystemSoundID inSystemSoundID,
    CFRunLoopRef  inRunLoop, CFStringRef  inRunLoopMode, AudioServicesSystemSoundCompletionProc  inCompletionRoutine,   void*  inClientData)方法注册回调函数。
    调用AudioServicesPlaySystemSound(SystemSoundID inSystemSoundID) 或者AudioServicesPlayAlertSound(SystemSoundID inSystemSoundID) 方法播放音效（后者带有震动效果）。
 *
 */
- (void)playSound:(NSString *)name {
    /**
     *  Fou    
     *
     *  参数1：文件的URL
     *  参数2：声音的ID
     */
    NSString *path = [[NSBundle mainBundle]pathForResource:name ofType:nil];
    NSURL *url = [NSURL fileURLWithPath:path];
    
    //NSURL *fielURL = [NSBundle mainBundle] URLForResource: withExtension:
    SystemSoundID soundID = 0;
    
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url), &soundID);
    /**
     *  参数4：当音频播放完以后，会调用的方法
     */
    AudioServicesAddSystemSoundCompletion(soundID, NULL, NULL, soundCompleteCallback, NULL);
    //播放音效
    AudioServicesPlaySystemSound(soundID);
    
    //播放音效并震动
    AudioServicesPlayAlertSound(soundID);
    
}

void soundCompleteCallback(SystemSoundID soundID,void * clientData){
    NSLog(@"播放完成...");
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self playSound:@"paipai.caf"];
}

//音乐：歌曲。。。mp3
- (void)playMusic:(NSString *)name {
    
}

- (IBAction)play:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (sender.selected) {
        
        [self.play play];
        [self startTime];
        _timer.fireDate=[NSDate distantPast];//恢复定时器
        NSLog(@"总时长：%f",self.play.duration);
        NSLog(@"当前播放时长：%f",self.play.currentTime);
    } else {
        [self.play pause];
        
        [self stopTime];
    }
    
}

- (IBAction)upClick:(id)sender {
    [self createPlay:@"雨一直下.mp3"];
    
}

- (IBAction)downClick:(id)sender {
    [self createPlay:@"雨一直下.mp3"];
}

- (void)viewDidDisappear:(BOOL)animated {
    [self stopTime];
}

- (void)startTime {
    if (!_timer) {
        _timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
        
    }
}

- (void)onTimer {
    //self.play.rate = 1;
    float progress= self.play.currentTime /self.play.duration;
    self.slider.value = progress;
    NSLog(@"总时长：%f",self.play.duration);
    NSLog(@"当前播放时长：%f",self.play.currentTime);
}

- (void)stopTime {
    if (_timer) {
//        [_timer invalidate];
//        _timer = nil;
        //暂停定时器，注意不能调用invalidate方法，此方法会取消，之后无法恢复
        _timer.fireDate = [NSDate distantFuture];
        
    }
}

//当前时长 = 总时长*比例
- (IBAction)sliderChang:(id)sender {
    self.play.currentTime = self.play.duration*self.slider.value;
}




@end



